﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SheepCommon.API.LambWeanings;

namespace SheepCommon.API.Crossbreedings
{
    public class CrossbreedingSearchHander : IRequestHandler<CrossbreedingSearchCommand, ResultPage<CrossbreedingSearchDto>>
    {
        private readonly IBaseRepository<Crossbreeding> _crossbreedingRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IMapper _mapper;

        public CrossbreedingSearchHander(IBaseRepository<Crossbreeding> crossbreedingRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IMapper mapper)
        {
            _crossbreedingRepository = crossbreedingRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 后裔转钟查询显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<CrossbreedingSearchDto>> Handle(CrossbreedingSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<CrossbreedingSearchDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var crossbreeding = await (from a in await _crossbreedingRepository.GetAll()
                                           join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                           join c in await _fieldRepository.GetAll() on a.FieldId equals c.FieldId
                                           select new CrossbreedingSearchDto()
                                           {
                                               CrossbreedingId = a.CrossbreedingId,
                                               BredSheepNo = a.BredSheepNo,
                                               CottageId = b.CottageId,
                                               CottageName = b.CottageName,
                                               FieldId = c.FieldId,
                                               FieldName = c.FieldName,
                                               CurrentBuildings = a.CurrentBuildings,
                                               CurrentColumns = a.CurrentColumns,
                                               Sex = a.Sex,
                                               AgeTransfer = a.AgeTransfer,
                                               CreatedOwnerId = a.CreatedOwnerId,
                                               CreatedTime = a.CreatedTime,
                                               UpdatedOwnerId = a.UpdatedOwnerId,
                                               UpdatedTime = a.UpdatedTime,
                                               CrossbreedingTime = a.CrossbreedingTime,
                                           }).ToListAsync();
                if (request.StartCrossbreedingTime != null)
                {
                    crossbreeding = crossbreeding.Where(p => p.CrossbreedingTime >= request.StartCrossbreedingTime).ToList();
                }
                if (request.EndCrossbreedingTime != null)
                {
                    crossbreeding = crossbreeding.Where(p => p.CrossbreedingTime <= request.EndCrossbreedingTime).ToList();
                }
                if (!string.IsNullOrEmpty(request.BredSheepNo))
                {
                    crossbreeding = crossbreeding.Where(p => p.BredSheepNo == request.BredSheepNo).ToList();
                }

                result.TotalCount = crossbreeding.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                crossbreeding = crossbreeding.OrderByDescending(c => c.CrossbreedingId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = crossbreeding;
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
